/**********************************************************************************************
Do-file:		ThreeYearHorizon.do
Date:			4/5/24
Author:			Stephanie Karol
Description:	This file produces the output for Section 6.

***********************************************************************************************/
clear 
set more off

use "cleaned-990-diff3.dta", replace

/*Clean data*/
* Contribcurrent in the raw data includes all contributions, regardless of source
* We are interested in looking at private contributions separately from government grants
* This line nets out government grants from total contributions
replace contribcurrent = contribcurrent - govgrantsrev
drop if contribcurrent < 0
* drop non-501c3
keep if exempt501c3 == "501c3"

/*merge in additional data*/
* fisyrendmo = yyyy-mm
gen fisyrendmo = substr(endyear,1,7)
gen year = fisyr

merge m:1 fisyrendmo using "cpifisyr.dta", keep(1 3)
drop year _merge

merge m:1 ein using "ntee.dta", keep(1 3)
drop _merge
encode ntmaj5, generate(ntee5)
replace ntee5 = . if ntmaj5 == ""

replace ntee5 = 1 if ntmaj5 == "AR"
replace ntee5 = 2 if ntmaj5 == "ED"
replace ntee5 = 3 if ntmaj5 == "HE"
replace ntee5 = 4 if ntmaj5 == "HU"
replace ntee5 = 5 if ntmaj5 == "OT"
replace ntee5 = . if ntmaj5 == ""

gen ntar = (ntmaj5 == "AR")
gen nted = (ntmaj5 == "ED")
gen nthe = (ntmaj5 == "HE")
gen nthu = (ntmaj5 == "HU")
gen ntot = (ntmaj5 == "OT")

* transform the data using: inverse hyperbolic sine, log, and indicators for strictly positive values. first deflate financial variables.

foreach var of varlist totalrevcurrent totalprogservexp totfundexp contribcurrent totalassetsbegyear salariescurrent govgrantsrev {
	replace `var' = `var'/(cpiu12mma/100)
}

foreach var of varlist totalrevcurrent totalprogservexp totfundexp contribcurrent  salariescurrent totalassetsbegyear totemployee govgrantsrev {
	gen log`var' = ln(`var')
	gen prob`var' = (`var' > 0)
	gen ihs`var' = ln(`var'+sqrt(`var'^2+1))	
}

gen after = (substr(endyear,1,4)=="2020" |substr(endyear,1,4)=="2021" |substr(endyear,1,4)=="2022")
replace after = 1 if ((fisyr == 2019) & (filingmonth == "12"))


/*variables capturing any exposure to COVID vs. share of months (out of 12) of exposure. */
gen anyexposure = ((filingmonth != "12") & (filingmonth != "01") & (filingmonth != "02"))

gen junefilers = (filingmonth == "06")
replace junefilers = . if ((junefilers == 0) & (filingmonth != "12") & (filingmonth != "01") & (filingmonth != "02"))

gen anyafter = anyexposure*after
gen juneafter = junefilers*after

gen fymar = (filingmonth == "03")
gen fyapr = (filingmonth == "04")
gen fymay = (filingmonth == "05")
gen fyjun = (filingmonth == "06")
gen fyjul = (filingmonth == "07")
gen fyaug = (filingmonth == "08")
gen fysep = (filingmonth == "09")
gen fyoct = (filingmonth == "10")
gen fynov = (filingmonth == "11")

foreach var of varlist fy* {
    gen `var'after = `var'*after
}

* renaming some variables to keep the names short and legal
rename contribcurrent contribcur
rename totalprogservexp pse
rename totfundexp fund
rename salariescurrent salcur
rename totemployee totemp
rename govgrantsrev ggr
rename totalrevcurrent trc

rename logcontribcurrent logcontribcur
rename logtotalprogservexp logpse
rename logtotfundexp logfund
rename logsalariescurrent logsalcur
rename logtotemployee logtotemp
rename loggovgrantsrev logggr
rename logtotalrevcurrent logtrc

rename probcontribcurrent probcontribcur
rename probtotalprogservexp probpse
rename probtotfundexp probfund
rename probsalariescurrent probsalcur
rename probtotemployee probtotemp
rename probgovgrantsrev probggr
rename probtotalrevcurrent probtrc

rename ihscontribcurrent ihscontribcur
rename ihstotalprogservexp ihspse
rename ihstotfundexp ihsfund
rename ihssalariescurrent ihssalcur
rename ihstotemployee ihstotemp
rename ihsgovgrantsrev ihsggr
rename ihstotalrevcurrent ihstrc

label variable anyafter "Any Exposure $\cdot$ After"
label variable juneafter "June Filers $\cdot$ After"
label variable fyjunafter "June Filers $\cdot$ After"
label variable anyexposure "Any Exposure"
label variable junefilers "June Filers"
label variable fyjun "June Filers"
label variable after "After"

label variable ntar "Arts"
label variable nted "Education"
label variable nthe "Health"
label variable nthu "Human Services"
label variable ntot "Other"

xtset ein fisyr

/*Figure 6: Binary DDs, no controls */
foreach var of varlist contribcur pse fund salcur totemp ggr fund trc {
reghdfe ihs`var' anyafter after if (fisyr == 2018 | fisyr == 2021), a(ein) vce(cluster fips) 
est sto any`var'21 
}


mat fig6 = J(7,2,.)

local i = 1
foreach var of varlist contribcur pse fund trc salcur totemp ggr {
		qui est restore any`var'21
		mat fig6[`i',1]= e(b)[1,1]
		mat fig6[`i',2] = sqrt(e(V)[1,1])
		local i = `i'+1
}

preserve
svmat fig6
rename fig61 fig6b
rename fig62 fig6se
keep fig6*
drop if _n > 7
export delimited using "fig6.csv", replace
restore

* Sectors
foreach var of varlist contribcur pse fund trc salcur totemp ggr {
	foreach var2 of varlist ntar nted nthe nthu ntot {
	reghdfe ihs`var' anyafter after if (`var2'==1 & (fisyr == 2018 | fisyr == 2021)), a(ein) vce(cluster fips)
	est sto any`var'_`var2'21
}
}

mat anyout21 = J(35,2,.)

local i = 1
foreach var of varlist contribcur pse fund trc salcur totemp ggr {
	foreach var2 of varlist ntar nted nthe nthu ntot {
		qui est restore any`var'_`var2'21
		mat anyout21[`i',1]= e(b)[1,1]
		mat anyout21[`i',2] = sqrt(e(V)[1,1])
		local i = `i'+1
	}
}

preserve
svmat anyout21
rename anyout211 anyout21b
rename anyout212 anyout21se
keep anyout21*
drop if _n > 35
export delimited using "forgraphsbycategory21.csv", replace
restore

* Filing month
mat att_filingmonth21 = J(63,2,.)

local i = 1
foreach var1 of varlist fymar fyapr fymay fyjun fyjul fyaug fysep fyoct fynov {
    foreach var2 of varlist contribcur fund trc pse salcur totemp ggr {
		reghdfe ihs`var2' `var1'after after if (`var1' == 1 | anyexposure == 0 & (fisyr == 2018 | fisyr == 2021)), a(ein) vce(cluster fips)
		*est sto att_`var1'_`var2'
		mat att_filingmonth21[`i',1] = e(b)[1,1]
		mat att_filingmonth21[`i',2] = sqrt(e(V)[1,1])
		local i = `i'+1
	}
}

preserve
svmat att_filingmonth21
rename att_filingmonth211 att_filingmonth21b
rename att_filingmonth212 att_filingmonth21se
keep att_filingmonth*
drop if _n > 63
export delimited using "att_filingmonth21.csv", replace
restore


label variable anyafter "Any Exposure $\cdot$ After"
label variable after "After"

* Table version of Figure 6
esttab anycontribcur21 anyggr21 anytotemp21 anysalcur21 anypse21 using "binarydd21-nocontrols.tex", not se tex replace fragment label r2 mtitles("\shortstack{Private\\Contrib.}" "\shortstack{Gov.\\Grants}" "Employees" "Salaries" "\shortstack{Program\\Services}") b(%9.3f) se(%9.3f) star(* 0.10 ** 0.05 *** 0.01) order(anyafter after)
